package com.example.bottomnavigationkotlin.ui.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.bottomnavigationkotlin.R

class HomeMultiAdapter(
    private val items: List<HomeMultiItem>,
    private val onGridItemClick: (HomeGridItem) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_GRID = 0
        private const val TYPE_DECISION = 1
        private const val TYPE_BANNER = 2
        private const val TYPE_FEATURED = 3
    }

    override fun getItemViewType(position: Int): Int = when (items[position]) {
        is HomeMultiItem.Grid -> TYPE_GRID
        is HomeMultiItem.DecisionTool -> TYPE_DECISION
        is HomeMultiItem.Banner -> TYPE_BANNER
        is HomeMultiItem.Featured -> TYPE_FEATURED
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_GRID -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_grid_block, parent, false)
                GridHolder(view)
            }
            TYPE_DECISION -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_decision, parent, false)
                DecisionHolder(view)
            }
            TYPE_BANNER -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_banner, parent, false)
                BannerHolder(view)
            }
            TYPE_FEATURED -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_featured, parent, false)
                FeaturedHolder(view)
            }
            else -> throw IllegalArgumentException()
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val item = items[position]) {
            is HomeMultiItem.Grid -> (holder as GridHolder).bind(onGridItemClick)
            is HomeMultiItem.DecisionTool -> (holder as DecisionHolder).bind(item)
            is HomeMultiItem.Banner -> (holder as BannerHolder).bind(item)
            is HomeMultiItem.Featured -> (holder as FeaturedHolder).bind(item)
        }
    }

    override fun getItemCount() = items.size

    class GridHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val grid: RecyclerView = view.findViewById(R.id.recycler_home_grid_inner)
        fun bind(onGridItemClick: (HomeGridItem) -> Unit) {
            val items = listOf(
                HomeGridItem("股票", R.drawable.ic_stock),
                HomeGridItem("商品(期货)", R.drawable.ic_goods),
                HomeGridItem("数据中心", R.drawable.ic_data),
                HomeGridItem("决策", R.drawable.ic_decision),
                HomeGridItem("龙虎榜", R.drawable.ic_rank),
                HomeGridItem("同花顺热榜", R.drawable.ic_hot),
                HomeGridItem("涨停聚焦", R.drawable.ic_focus),
                HomeGridItem("投顾服务", R.drawable.ic_service),
                HomeGridItem("k线训练营", R.drawable.ic_kline),
                HomeGridItem("应用商店", R.drawable.ic_store)
            )
            grid.layoutManager = androidx.recyclerview.widget.GridLayoutManager(itemView.context, 5)
            grid.adapter = HomeGridAdapter(items, onGridItemClick)
        }
    }

    class DecisionHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val title: TextView = view.findViewById(R.id.tv_decision_title)
        private val subtitle: TextView = view.findViewById(R.id.tv_decision_subtitle)
        fun bind(item: HomeMultiItem.DecisionTool) {
            title.text = item.title
            subtitle.text = item.subtitle
        }
    }

    class BannerHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val image: ImageView = view.findViewById(R.id.iv_banner)
        fun bind(item: HomeMultiItem.Banner) {
            image.setImageResource(item.imageRes)
        }
    }

    class FeaturedHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val title: TextView = view.findViewById(R.id.tv_featured_title)
        private val desc: TextView = view.findViewById(R.id.tv_featured_desc)
        private val image: ImageView = view.findViewById(R.id.iv_featured)
        fun bind(item: HomeMultiItem.Featured) {
            title.text = item.title
            desc.text = item.desc
            image.setImageResource(item.imageRes)
        }
    }
} 